package com.dao;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;

import com.entity.Users;
//使用注解代替sql语句的书写，更加方便观看
public interface UsersDao {

    @Select("select count(*) from users")
    public long selectCount();

    @Select("<script>"+ "SELECT count(*) " + "from users" + "<where>" +
            "<if test='username!=null'>" +
            "and username like CONCAT('%',#{username},'%')" +
            "</if>" +
            "</where>" +
            "</script>")
    public long queryCount(@Param("username")String username);

    @Select("select * from users order by id desc limit #{begin}, #{size}")
    public List<Users> selectList(@Param("begin") int begin, @Param("size") int size);

    @Select("<script>"+ "SELECT * " + "from users" + "<where>" +
            "<if test='username!=null'>" +
            "and username like CONCAT('%',#{username},'%')" +
            "</if>" +
            "</where>" +
            "order by id desc limit #{begin}, #{size}" +
            "</script>")
    public List<Users> queryList(@Param("begin") int begin, @Param("size") int size,@Param("username")String username);

    @Select("select * from users where id=#{id}")
    public Users select(int id);

    @Select("select * from users where username=#{username}")
    public Users selectByUsername(String username);

    @Select("select * from users where username=#{username} and password=#{password}")
    public Users selectByUsernameAndPassword(@Param("username") String username, @Param("password") String password);

    @Insert("insert into users (username,password,name,phone,address) "
            + "values (#{username},#{password},#{name},#{phone},#{address})")
    @SelectKey(keyProperty = "id", statement = "SELECT LAST_INSERT_ID()", before = false, resultType = Integer.class)
    public boolean insert(Users user);

    @Update("update users set name=#{name},phone=#{phone},address=#{address} where id=#{id}")
    public boolean update(Users user);

    @Update("update users set password=#{password} where id=#{id}")
    public boolean updatePassword(@Param("id") int id, @Param("password") String password);

    @Update("delete from users where id=#{id}")
    public boolean delete(int id);

    @Update("update users set point=#{point} where id=#{id}")
    int updatePoint(Users users);
}